package com.leetcode;

/**
 * 868. 二进制间距
 * 位运算
 *
 * @author fy
 * @date 2022/4/24 11:56
 */
public class Solution868_1 {

    public int binaryGap(int n) {
        int last = -1;
        int ans = 0;
        for (int i = 0; n != 0; ++i) {
            if ((n & 1) == 1) {
                if (last != -1) {
                    ans = Math.max(ans, i - last);
                }
                last = i;
            }
            n >>= 1;
        }
        return ans;
    }

}
